<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <div>
    <BasicTable @register="registerTable" @row-click="handleTestDataChildRowClick">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button type="primary" v-auth="'setting:price:edit'" @click="handleExport">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-popconfirm
          title="是否确认批量删除数据？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleBatchDelete()"
        >
          <a-button type="primary" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:delete-outlined" /> {{ t('批量删除') }}
          </a-button>
        </a-popconfirm>
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.testInput }}
        </a>
      </template> -->
    </BasicTable>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsWecharCusInvPriceRouteValidList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    treeData,
    listCusInvPriceRouteValidData,
    listCusInvPriceRouteValidDelete,
  } from '/@/api/setting/price/wecharCusInvPriceRoute';

  import { basCusTreeData } from '/@/api/bas/cus/basCus';
  import { FormProps } from '/@/components/Form';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';

  const { t } = useI18n('setting.price.wecharCusInvPriceRouteValid');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('价格及协同路线(已生效)'),
  };
  //配置表单内容
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('终端客户'),
        field: 'endCusCode',
        // fieldLabel: 'endBasCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: basCusTreeData,
          params: { isAll: true },
          allowClear: true,
          immediate: true,
        },
      },
      {
        label: t('产品编码'),
        field: 'invCode',
        component: 'Input',
        componentProps: {
          allowClear: true,
        },
      },
      {
        label: t('产品名称'),
        field: 'basInv.cinvname',
        component: 'Input',
        componentProps: {
          allowClear: true,
        },
      },
    ],
  };
  //配置表格表头菜单
  const tableColumns: BasicColumn[] = [
    {
      title: t('终端客户'),
      dataIndex: 'endCusCode',
      key: 'a.endCusCode',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('终端客户名称'),
      dataIndex: 'endBasCus.name',
      key: 'a.endBasCus.name',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('产品编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('产品名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.unitName',
      key: 'inv.unit_name',
      sorter: true,
      width: 60,
      align: 'center',
    },
    // {
    //   title: t('单价'),
    //   dataIndex: 'fprice',
    //   key: 'a.fprice',
    //   sorter: true,
    //   width: 80,
    //   align: 'left',
    // },
    {
      title: t('协同单价'),
      dataIndex: 'routePrice',
      key: 'a.route_price',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('协同路线'),
      dataIndex: 'croute',
      key: 'a.croute',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('协同描述'),
      dataIndex: 'routeDesc',
      key: 'a.route_desc',
      sorter: true,
      width: 230,
      align: 'left',
    },
    // {
    //   title: t('协同账套'),
    //   dataIndex: 'caccId',
    //   key: 'a.caccId',
    //   sorter: true,
    //   width: 180,
    //   align: 'left',
    // },
    // {
    //   title: t('账套名称'),
    //   dataIndex: 'caccId',
    //   key: 'a.caccId',
    //   sorter: true,
    //   width: 180,
    //   align: 'left',
    //   dictType: 'u8_accid',

    // },
  ];
  //配置表格右边操作按钮
  const actionColumn: BasicColumn = {
    width: 180,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除数据'),
        popConfirm: {
          title: t('是否确认删除数据'),
          // confirm: handleDelete.bind(this, { endCusCode: record.endCusCode,invCode:record.invCode }),
          confirm: handleDelete.bind(this, { selIds: record.id }),
        },
        auth: 'setting:price:edit',
      },
    ],
  };

  const [registerTable, { reload, getSelectRows, getForm }] = useTable({
    api: listCusInvPriceRouteValidData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns, //配置表格内容数组对象
    actionColumn: actionColumn, //配置表格右边操作按钮
    formConfig: searchForm, //配置表单内容
    showTableSetting: true, //表格右上角3个默认按钮
    useSearchForm: true, //表单是否展示
    canResize: true, //表格是否flex布局
    rowSelection: {
      type: 'checkbox',
    },
  });

  async function handleDelete(record: Recordable) {
    const res = await listCusInvPriceRouteValidDelete(record);
    showMessage(res.message);
    handleSuccess();
  }
  async function handleBatchDelete() {
    if (getSelectRows().length) {
      try {
        let selIds = '';
        getSelectRows().forEach((item) => {
          selIds += item.id + ',';
        });
        const res = await listCusInvPriceRouteValidDelete({ selIds });
        showMessage(res.message);
      } catch (error: any) {
        console.log('error', error);
      } finally {
        handleSuccess();
      }
    } else {
      showMessage('请先选择数据！！');
    }
  }
  function handleTestDataChildRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  // 导出
  async function handleExport() {
    if (!getForm().getFieldsValue().endCusCode) {
      showMessage('请先选择终端客户！！', 'warning');
      return;
    }
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/setting/price/wecharCusInvPriceRouteValid/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  function handleSuccess() {
    reload();
  }

  onMounted(async () => {
    handleCuscode();
  });
  async function handleCuscode() {
    await getForm().setFieldsValue({
      endCusCode: router.currentRoute.value.query.cusCode,
    });
    reload();
    await getForm().setFieldsValue({
      endCusCode: router.currentRoute.value.query.cusCode,
    });
  }
</script>
